// pagesMine/pages/withdraw/withdraw.js
import ajax from "../../../utils/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    isSubmit: false,
    formData: {
      accountNumber: null, // 账号 
      name: null, // 姓名 
      profit: null, // 金额 
      receiptMethod: null // 到账方式 
    }
  },
  inputChange(event) {
    this.data.formData.profit = event.detail.value
    this.isSubmitChange()
  },
  allEvent() {
    const surplusCommission = parseInt(this.data.userInfo.surplusCommission || '0')
    if (!surplusCommission) return ajax.showToast('余额不足')
    this.setData({ 'formData.profit': surplusCommission })
    this.isSubmitChange()
  },
  /**
   * 校验字段
   */
  isSubmitChange() {
    const { accountNumber, name, profit, receiptMethod } = this.data.formData;
    receiptMethod == 1 ? '' : ''
    let boo = true
    if (receiptMethod == 0) boo =!(profit)
    if (receiptMethod == 1) boo =!(profit && accountNumber && name)
    this.setData({ isSubmit: !boo })
  },
  /**
   * input 变化事件
   */
  formItemChange(event) {
    const { formData } = this.data
    const key = event.currentTarget.dataset.key
    const value = event.detail.value || event.detail
    formData[key] = value
    this.setData({ formData })
    this.isSubmitChange()
  },
  /**
    * 选择框事件
    */
  pickerChange(event) {
    const key = event.currentTarget.dataset.key
    const { formData } = this.data
    formData[key] = event.detail.startValue
    if (key == 'receiptMethodName') formData.receiptMethod = event.detail.startIndex;
    this.setData({ formData })
    this.isSubmitChange()
  },
  async getUserInfo() {
    const res = await ajax.getUserInfo()
    this.setData({ userInfo: res })
  },
  async onSubmit () {
    ajax.showLoading()
    await ajax.post('/user/bill/withdrawal', this.data.formData).finally(() => wx.hideLoading())
    await ajax.showSuccess('操作成功')
    wx.navigateBack()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})